package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;

/**
 * 我要报名页面
 * Created by ${wangtao} on 2017/3/6.
 */

public class FloorEventPublishActivity extends BaseActivity implements View.OnClickListener,ContributorView {

    private EditText fragment_wybm_name;//姓名
    private TextView fragment_wybm_sex;//性别
    private EditText fragment_wybm_lxfs;//电话
    private EditText fragment_wybm_content;//备注信息
    private Button fragment_lyhdbm_enter;//立即报名
    private ImageView fragment_mycp_back;//返回按钮
    private RelativeLayout fragment_wybm_LltSex;//选择性别
    private Context context;
    private PopupWindow popupWindow;
    private View mpopupWindowView;
    private TextView tv_cancel;
    private TextView tv_man;
    private TextView tv_woman;
    private String uid;
    private String id;
    private MyToast myToast;
    private ContributorPresenter contributorPresenter;
    private final String BUILDINGACTIVITYSIGNUPFLAG="BUILDINGACTIVITYSIGNUPFLAG";
    private TextView tv_headtitle;
    private String cidNumber;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flooreventpublish_layout);
        context=this;
        myToast=new MyToast(context);
        contributorPresenter=new ContributorPresenter(this,context);
        id=getIntent().getStringExtra("id");
        cidNumber=getIntent().getStringExtra("cid");
        type=getIntent().getIntExtra("type",type);
        uid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
        initView();
        initPopupWindow();
    }

    private void initView()
    {
        fragment_wybm_name=(EditText) findViewById(R.id.fragment_wybm_name);//姓名
        fragment_wybm_sex=(TextView) findViewById(R.id.fragment_wybm_sex);//性别
        fragment_wybm_lxfs=(EditText) findViewById(R.id.fragment_wybm_lxfs);//电话
        fragment_wybm_content=(EditText) findViewById(R.id.fragment_wybm_content);//备注信息
        fragment_lyhdbm_enter=(Button) findViewById(R.id.fragment_lyhdbm_enter);//立即报名
        fragment_mycp_back=(ImageView) findViewById(R.id.regist_back);//返回按钮
        fragment_wybm_LltSex=(RelativeLayout) findViewById(R.id.fragment_wybm_LltSex);//选择性别
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("我要报名");
        fragment_lyhdbm_enter.setOnClickListener(this);
        fragment_mycp_back.setOnClickListener(this);
        fragment_wybm_LltSex.setOnClickListener(this);
        tv_headtitle.requestFocus();
        tv_headtitle.setFocusable(true);
        tv_headtitle.setFocusableInTouchMode(true);
    }

    /**
     * 初始化popupwindow
     */
    private void initPopupWindow() {
        mpopupWindowView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.sexinfo, null);
        popupWindow = new PopupWindow(mpopupWindowView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);// 全屏显示，将内容设置在底部
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        tv_cancel = (TextView) mpopupWindowView.findViewById(R.id.tv_cancelsex);
        tv_cancel.setOnClickListener(this);
        tv_woman = (TextView) mpopupWindowView.findViewById(R.id.tv_takephotosex);
        tv_woman.setOnClickListener(this);
        tv_man = (TextView) mpopupWindowView.findViewById(R.id.tv_photosex);
        tv_man.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
            case R.id.fragment_wybm_LltSex:
                View view = getWindow().peekDecorView();
                if (view != null) {
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
                if (!popupWindow.isShowing()) {
                    popupWindow.showAtLocation(findViewById(R.id.fragment_wybm_LltSex), Gravity.BOTTOM, 0, 0);// 在屏幕居中，无偏移
                } else {
                    popupWindow.dismiss();
                }
                break;
            case R.id.tv_cancelsex:
                popupWindow.dismiss();
                break;
            // woman
            case R.id.tv_takephotosex:
                popupWindow.dismiss();
                fragment_wybm_sex.setText("女");
                break;
            // man
            case R.id.tv_photosex:
                popupWindow.dismiss();
                fragment_wybm_sex.setText("男");
                break;
            case R.id.fragment_lyhdbm_enter:
                publishEvent(uid,cidNumber,id,fragment_wybm_name.getText().toString(),fragment_wybm_sex.getText().toString(),fragment_wybm_lxfs.getText().toString(),fragment_wybm_content.getText().toString());
                break;
        }
    }

    private void publishEvent(String uid,String cid,String bid,String name,String sex,String mobile,String content)
    {
        if(mobile.isEmpty())
        {
            myToast.showShort("联系方式为空");
        }else
        {
            if(Tools.isMobile(mobile))
            {
                if(name.isEmpty()|| sex.isEmpty()|| mobile.isEmpty()||content.isEmpty())
                {
                    myToast.showShort("请填写完整信息");
                }else {
                    int sexid=0;
                    if (sex.equals("男")) {
                        sexid=1;
                    }else if (sex.equals("女")){
                        sexid=0;
                    }
                    contributorPresenter.getBuildingActivitySignup(uid,cid,id,name,sexid,mobile,content,type,BUILDINGACTIVITYSIGNUPFLAG);

                }

            }else
            {
                myToast.showShort("手机号码不合法");
            }
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {
        if(flag.equals(BUILDINGACTIVITYSIGNUPFLAG))
        {
            fragment_lyhdbm_enter.setEnabled(false);
        }

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(BUILDINGACTIVITYSIGNUPFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    myToast.showShort("报名成功");
                    finish();
                }else
                {
                    myToast.showShort("操作失败");
                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
        if(flag.equals(BUILDINGACTIVITYSIGNUPFLAG))
        {
            fragment_lyhdbm_enter.setEnabled(true);
        }
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

}
